import {useState} from "react";

// 管理员设置全部考勤规则

interface UseAllSettings {
    allSettings: (morning_start_time: string, evening_end_time: string) => Promise<void>;
    isAllLoading: boolean;
    allError: string | null;
}

export function useAllSettings(): UseAllSettings {
    const [isAllLoading, setIsLoading] = useState(false);
    const [allError, setError] = useState<string | null>(null);

    const token = localStorage.getItem('token');
    const allSettings = async (morning_start_time: string, evening_end_time: string): Promise<void> => {
        setIsLoading(true);
        setError(null);
        try {
            if (!token) {
                setError('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/admin/setAllAttendanceRules', {
                method: 'POST',
                headers: {
                    'Authorization': token!,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({morning_start_time, evening_end_time}),
            });

            if (!response.ok) {
                throw new Error('设置失败');
            }
        } catch (allError) {
            setError((allError as Error).message);
        } finally {
            setIsLoading(false);
        }
    };
    return {allSettings, isAllLoading, allError};
}
